package dolf.objects.weapons.Nails;
import dolf.game.*;
import dolf.objects.weapons.*;
import dolf.objects.Ball;
import dolf.objects.CircleObject;
import dolf.objects.Hole;
import dolf.objects.ImageStore;
import dolf.objects.Vector;
import dolf.objects.WorldObject;
import dolf.window.SystemMessagesHandler;

import java.awt.Graphics2D;


public class Nails extends CircleObject  implements Weapon, java.io.Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int Price = 70;
	public static final String Name = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Nails.lang.lang").getString("name");
	public static final String WeaponType = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Nails.lang.lang").getString("type");
	public static final String Description = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Nails.lang.lang").getString("description");
	public static final javax.swing.ImageIcon Icon = ImageStore.getInstance().getIcon("dolf/objects/weapons/Nails/gfx/icon.png");
	
	private boolean used;
	private Player pl;
	private int z;
	private boolean inititalized = false;
	
	
	public Nails(){
		super(null,13,false,true);	
	}
	public void initialize(Player _pl){
		if(this.inititalized){
			return;
		}
		this.inititalized = true;
		this.used = false;
		this.pl = _pl;
		this.pl.setWeaponState();
		this.z = 20;
	}
	public void deInitialize(){
		this.pl = null;
		this.inititalized = false;
	}
	
	public javax.swing.ImageIcon getIcon() {
		return Icon;//ImageStore.getInstance().getIcon("dolf/objects/weapons/Nails/gfx/icon.png");
	}

	public void paint(Graphics2D g){
		g.drawImage(ImageStore.getInstance().getImage("dolf/objects/weapons/Nails/gfx/icon.png"), this.getPosition().getIntX()-this.getRadius(), this.getPosition().getIntY()-this.getRadius(), null);
	}
	public void execute(){
		if(Level.getInstance().mousePressed()&&pl.getBall().isMoving()&&!used){
			this.setPosition(pl.getBall().getPosition().sub(pl.getBall().getMotion().setLength(this.getRadius()+pl.getBall().getRadius()+1)));
			this.setMotion(new Vector(0,0));
			if(throwable()){
				Level.getInstance().registerObject(this);
				this.used = true;
				this.pl.setWeaponState();
			} else {
				System.out.println("abwurf nicht moegl.");
			}
		}
		/*this.setPosition(pl.getBall().getPosition().sub(pl.getBall().getMotion().setLength(this.getRadius()+pl.getBall().getRadius())));
		this.setMotion(new Vector(0,0));
		Level.getInstance().registerObject(this);*/
	}
	public boolean newMotion(WorldObject object){
		boolean ret = true;
		if(object instanceof Ball){
			Ball obj = (Ball) object;
			if(obj.getPlayer().getHP()>1){
				this.pl.addDamage(1);
				obj.getPlayer().setHP(obj.getPlayer().getHP()-1);
				obj.setMotion(this.changeSpeed(object));
			} else {
				this.pl.addDamage(1);
				obj.getPlayer().setHP(obj.getPlayer().getHP()-1);
				ret = false;
			}
			if(--z <= 0){
				Level.getInstance().removeObject(this);
				this.setCollitabe(false);
			}
		}
		return ret;
	}
	
	private Vector changeSpeed(WorldObject obj){
		double x,y;
		x = obj.getMotion().getX()/3*Math.random() *Math.signum(Math.random() -0.5);
		y = obj.getMotion().getY()/3*Math.random() *Math.signum(Math.random() -0.5);
		return new Vector(obj.getMotion().getX()+x,obj.getMotion().getY()+y);
	}
	
	public String getName() {
		return Name;
		/*if (messages == null) loadProperties();
		return messages.getString("name");*/
	}
	
	public String getDescription() {
		return Description;
		/*if (messages == null) loadProperties();
		return messages.getString("description");*/
	}
	public String getType(){
		return WeaponType;
	}
	
	public int getPrice() {
		return Price;
	}
	public Player getPlayer(){
		return this.pl;
	}
	public void remove(){
		pl.removeWeapon(this);
		pl.setAktWeapon(null);
	}
	public double getState(){
		if(this.used){
			return 0;
		}else{
			return 1;
		}
	}
		private boolean throwable(){
		for(int i = 0; i < Level.getInstance().getObjects().size();i++){
			WorldObject obj = Level.getInstance().getObjects().get(i);
			if(obj != pl.getBall() && this.kollision(obj)==0 ){
				return false;
			}
		}
		for(int i = 0; i< Level.getInstance().getHoles().size();i++){
			Hole obj = Level.getInstance().getHoles().get(i);
			if(this.kollision(obj)==0 ){
				return false;
			}
		}
		return true;
	} 
}
